package com.topfinance.test.xml;


import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.StringReader;
import java.io.StringWriter;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.transform.stream.StreamSource;

import com.cnaps2.xml.iso20022.pacs.v00800102.Document;
import com.cnaps2.xml.iso20022.pacs.v00800102.ObjectFactory;

public class Jaxb2Util {
	public static final String ALL_MSG_TYPES[][] = new String[][] {
			{ "com.cnaps2.xml.iso20022.pacs.v00800102", "ibps.101.001.01", "FIToFICstmrCdtTrf", "IO" },
			{ "com.cnaps2.xml.iso20022.pacs.v00200103", "ibps.102.001.01", "FIToFIPmtStsRpt", "IO" },
			{ "com.cnaps2.xml.iso20022.pacs.v00300102", "ibps.103.001.01", "FIToFICstmrDrctDbt", "IO" },
			{ "com.cnaps2.xml.iso20022.pacs.v00200103", "ibps.104.001.01", "FIToFIPmtStsRpt", "IO" },
			{ "com.cnaps2.xml.iso20022.pacs.v00800102", "ibps.105.001.01", "FIToFICstmrCdtTrf", "IO" },
			{ "com.cnaps2.xml.iso20022.pacs.v00200103", "ibps.106.001.01", "FIToFIPmtStsRpt", "IO" },
			{ "com.topfinance.xml.camt.v05600101", "ibps.307.001.01", "FIToFIPmtCxlReq", "IO" },
			{ "com.topfinance.xml.pacs.v00200103", "ibps.308.001.01", "FIToFIPmtStsRpt", "IO" },
			{ "com.topfinance.xml.camt.v05400102", "saps.601.001.01", "BkToCstmrDbtCdtNtfctn", "IO" },
			{ "com.topfinance.xml.camt.v05300102", "saps.603.001.01", "BkToCstmrStmt", "IO" }
			};

	/**//*
		 * @param catalogType is the object that is used to build xml @param
		 * xmlDocument is the file where xml to output
		 */
	public static void buildXml(Document webFlow, File xmlDocument) {
		try {
			JAXBContext jaxbContext = JAXBContext.newInstance(getPackageByMSGId("001"));
			Marshaller marshaller = jaxbContext.createMarshaller();
			// marshaller.setProperty(Marshaller.JAXB_ENCODING,Constants.ENCODING_GBK);
			marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
			ObjectFactory factory = new ObjectFactory();
			JAXBElement<Document> webFlowElement = factory.createDocument(webFlow);
			marshaller.marshal(webFlowElement, new FileOutputStream(xmlDocument));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			// log.error("Can not find file.",e);
		} catch (JAXBException e) {
			e.printStackTrace();
			// log.error("Generating xml file failed.",e);
		}
	}

	/**
	 * 
	 * @param webFlow
	 * @return
	 */
	public static String buildXml(Document webFlow) {
		String rtnStr = "";
		try {
			JAXBContext jaxbContext = JAXBContext.newInstance(getPackageByMSGId("ibps.101.001.01"));
			Marshaller marshaller = jaxbContext.createMarshaller();
			// marshaller.setProperty(Marshaller.JAXB_ENCODING,Constants.ENCODING_GBK);
			marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
			ObjectFactory factory = new ObjectFactory();
			JAXBElement<Document> webFlowElement = factory.createDocument(webFlow);
			StringWriter sw = new StringWriter();
			marshaller.marshal(webFlowElement, sw);
			rtnStr = sw.toString();
		} catch (Exception e) {
			e.printStackTrace();
			// log.error("Generating xml file failed.",e);
		}
		return rtnStr;
	}

	/**//*
		 * @param xmlDocument the xml file to build object @return the built
		 * object
		 */
	public static Document buildObject(File xmlDocument) {
		JAXBElement<Document> webFlowElement = null;
		try {
			JAXBContext jaxbContext = JAXBContext.newInstance(getPackageByMSGId("ibps.101.001.01"));
			Unmarshaller unMarshaller = jaxbContext.createUnmarshaller();
			webFlowElement = (JAXBElement<Document>) unMarshaller.unmarshal(xmlDocument);
		} catch (Exception e) {
			e.printStackTrace();
			// log.error("Parsing xml to Object failed.",e);
		}
		return webFlowElement.getValue();
	}

	public static Document buildObject(String xmlDataStr) {
		JAXBElement<Document> webFlowElement = null;
		try {
			JAXBContext jaxbContext = JAXBContext.newInstance(getPackageByXML(xmlDataStr));
			Unmarshaller unMarshaller = jaxbContext.createUnmarshaller();
			webFlowElement = (JAXBElement<Document>) unMarshaller.unmarshal(new StreamSource(new StringReader(
					xmlDataStr)));
		} catch (Exception e) {
			e.printStackTrace();
			// log.error("Parsing xml to Object failed.",e);
		}
		return webFlowElement.getValue();
	}

	public static String getPackageByXML(String xmlDataStr) {
		for (int i = 0; i <= ALL_MSG_TYPES.length; i++) {
			int index = xmlDataStr.indexOf(ALL_MSG_TYPES[i][2]);
			if (index > -1 && index < 20)
				return ALL_MSG_TYPES[i][0];
		}
		return null;
	}

	public static String getPackageByMSGId(String msgId) {
		for (int i = 0; i <= ALL_MSG_TYPES.length; i++) {
			if (ALL_MSG_TYPES[i][1] == msgId)
				return ALL_MSG_TYPES[i][0];
		}
		return null;
	}

	public static void main(String[] args) {
		File file = new File("D:\\bankConnector\\sample\\iso20022\\ibps.101.001.01.xml");
		Document aDocument = Jaxb2Util.buildObject(file);
		//System.out.println(aDocument.getCommercialDraftRegisterRequest().getAccptr().getAcct().getAcctSvcr());
		//System.out.println("from aDocument:" + Jaxb2Util.buildXml(aDocument));
		//Jaxb2Util.buildXml(aDocument,file);
		System.out.println("from aDocument:\n" + Jaxb2Util.buildXml(aDocument));
	}
}

